package cn.itsub.sodo.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;


/**
 * Game工具类
 *
 * @author Mr.Xia
 * @version 1.4
 * www.zhenzhigu.com
 */
public class ImageUtil {


    public static BufferedImage opacity(final BufferedImage bufferedimage, float alpha) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        BufferedImage img = new BufferedImage(bufferedimage.getWidth(), bufferedimage.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2d = img.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        graphics2d.drawImage(bufferedimage, 0, 0, w, h, null);
        graphics2d.dispose();
        return img;
    }

    /**
     * 旋转图片为指定角度
     *
     * @param bufferedimage 目标图像
     * @param degree        旋转角度
     * @return
     */
    public static BufferedImage rotateImage(final BufferedImage bufferedimage, final float degree) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        int q = (int) Math.hypot(w, h);
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img = new BufferedImage(q, q, type);
        Graphics2D graphics2d = img.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate(Math.toRadians(degree), q / 2, q / 2);
        graphics2d.drawImage(bufferedimage, (q - w) / 2, (q - h) / 2, null);
        graphics2d.dispose();
        return img;
    }

    /**
     * 变更图像为指定大小
     *
     * @param bufferedimage 目标图像
     * @param w             宽
     * @param h             高
     * @return
     */
    public static BufferedImage resizeImage(final BufferedImage bufferedimage, final int w, final int h) {
        int type = bufferedimage.getColorModel().getTransparency();
        BufferedImage img = new BufferedImage(w, h, type);
        Graphics2D graphics2d = img.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage
                .getWidth(), bufferedimage.getHeight(), null);

        graphics2d.setColor(Color.WHITE);
        graphics2d.dispose();
        return img;
    }

    /**
     * 水平翻转图像
     *
     * @param bufferedimage 目标图像
     * @return
     */
    public static BufferedImage flipImage(final BufferedImage bufferedimage) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        BufferedImage img;
        Graphics2D graphics2d;
        (graphics2d = (img = new BufferedImage(w, h, bufferedimage
                .getColorModel().getTransparency())).createGraphics())
                .drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
        graphics2d.dispose();
        return img;
    }


    /*
     *
     * 镜像处理，输入image和方式，返回翻转的新image
     * flipX表示上下翻转，flipY表示左右翻转
     */
    public static BufferedImage flip(BufferedImage img, boolean flipX,boolean flipY) {
        try {
            int w = img.getWidth();
            int h = img.getHeight();
            int type = img.getColorModel().getTransparency();
            BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight(), type);
            int[][] datas = new int[w][h];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    datas[j][i] = img.getRGB(j, i);
                }
            }
            int[][] tmps = new int[w][h];
            if (flipX) {
                for (int i = 0, a = h - 1; i < h; i++, a--) {
                    for (int j = 0; j < w; j++) {
                        tmps[j][a] = datas[j][i];
                    }
                }
            }
            if (flipY) {
                for (int i = 0; i < h; i++) {
                    for (int j = 0, b = w - 1; j < w; j++, b--) {
                        tmps[b][i] = datas[j][i];
                    }
                }
            }
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    bimg.setRGB(j, i, tmps[j][i]);
                }
            }

            return bimg;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static InputStream toInputStream(BufferedImage image) throws IOException {
        java.io.ByteArrayOutputStream output = new java.io.ByteArrayOutputStream();
        javax.imageio.ImageIO.write(image, "png", output);
        return new java.io.ByteArrayInputStream(output.toByteArray());
    }
}